<?php
require_once dirname(__FILE__) . '/metadata.php';
require_once dirname(__FILE__) . '/DbUtils.class.php';
require_once dirname(__FILE__) . '/includes/MiscUtils.class.php';

fetch();
function fetch() {
//  $url = 'http://openexchangerates.org/latest.json?app_id=c17483063b3a415ebff29abf4b195a48';
  $url = 'http://openexchangerates.org/latest.json?app_id=0836475cb208465582aef296f37e1b03';
  $obj = json_decode(file_get_contents($url));
  if (gettype($obj) != 'object') {
    return;
  }
  //Put $obj to database...
  $myPdo = DbUtils::createPdoInst();
  $cond_vals = NULL;
  $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'currencies_currencies', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmps) > 0) {
    foreach ($tmps as $tmp) {
      $myPdo->exec('UPDATE ' . TABLE_NAME_PREFIX_PLUGIN . 'currencies_currencies SET attr3 = 0 WHERE id = ' . $tmp->id);
      foreach ($obj->rates as $key => $val) {
        if ($tmp->attr1 == $key) {
          $tmp->attr3 = $val;
          $tmp->date1 = date('Y-m-d H:i:s', $obj->timestamp);
          DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'currencies_currencies', $tmp);
        }
      }
    }
  }
}
?>